using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using Entities;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
   public  class tbDoanhNghiepDAL
    {
        private SqlConnection cn = null;
        public tbDoanhNghiepDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbDoanhNghiep doanhnghiep)
        {
            SqlCommand cmd_dn = new SqlCommand("DoanhNghiep_Insert",cn);
            cmd_dn.CommandType = CommandType.StoredProcedure;
            cmd_dn.Parameters.AddWithValue("@TenDoanhNghiep",doanhnghiep.TenDoanhNghiep);
            cmd_dn.Parameters.AddWithValue("@DiaChi", doanhnghiep.DiaChi);
            cmd_dn.Parameters.AddWithValue("@DienThoai", doanhnghiep.DienThoai );
            cmd_dn.Parameters.AddWithValue("@Email", doanhnghiep.Email);           
            cmd_dn.ExecuteNonQuery();
        }
        public void Update(tbDoanhNghiep doanhnghiep)
        {
            SqlCommand cmd_dd = new SqlCommand("DoanhNghiep_Update", cn);
            cmd_dd.CommandType = CommandType.StoredProcedure;
            cmd_dd.Parameters.AddWithValue("@IDDoanhNghiep", doanhnghiep.IDDoanhNghiep);
            cmd_dd.Parameters.AddWithValue("@TenDoanhNghiep", doanhnghiep.TenDoanhNghiep);
            cmd_dd.Parameters.AddWithValue("@DiaChi", doanhnghiep.DiaChi);
            cmd_dd.Parameters.AddWithValue("@DienThoai", doanhnghiep.DienThoai);
            cmd_dd.Parameters.AddWithValue("@Email", doanhnghiep.Email);            
          
            cmd_dd.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cmd_dd = new SqlCommand("DoanhNghiep_Delete", cn);
            cmd_dd.CommandType = CommandType.StoredProcedure;
            cmd_dd.Parameters.AddWithValue("@IDDoanhNghiep", iD);
            cmd_dd.ExecuteNonQuery();
        }
        public tbDoanhNghiep.tbDoanhNghiepCollection Get(string where, int pageindex, int pagesize, string orderby, string orderdirection, out int totalrecords)
        {
            SqlCommand cmd_dd = new SqlCommand("DoanhNghiep_Get", cn);
            cmd_dd.CommandType = CommandType.StoredProcedure;
            cmd_dd.Parameters.AddWithValue("@Where", where);
            cmd_dd.Parameters.AddWithValue("@PageIndex", pageindex);
            cmd_dd.Parameters.AddWithValue("@PageSize", pagesize);
            cmd_dd.Parameters.AddWithValue("@OrderBy", orderby);
            cmd_dd.Parameters.AddWithValue("@OrderDirection", orderdirection);
            cmd_dd.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
            SqlDataReader dr = cmd_dd.ExecuteReader();
            tbDoanhNghiep.tbDoanhNghiepCollection ddcollection = new tbDoanhNghiep.tbDoanhNghiepCollection();
            while (dr.Read())
            {
                tbDoanhNghiep dd = new tbDoanhNghiep();
                dd.IDDoanhNghiep = int.Parse(dr.GetValue(0).ToString());
                dd.TenDoanhNghiep = dr.GetValue(1).ToString();
                dd.DiaChi = dr.GetValue(2).ToString();
                dd.DienThoai = dr.GetValue(3).ToString();
                dd.Email = dr.GetValue(4).ToString();
              
                
                ddcollection.Add(dd);
            }
            dr.Close();
            totalrecords = int.Parse(cmd_dd.Parameters["@TotalRecords"].Value.ToString());
            return ddcollection;
        }
    }
}
